package com.itic.mall.system.goods.skucoupon.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.NumericHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.inter.goods.skucoupon.persistence.dao.SkuCouponDao;
import com.itic.mall.system.inter.goods.skucoupon.persistence.model.SkuCoupon;
import com.itic.mall.system.inter.goods.skucoupon.persistence.model.SkuCouponExample;
import com.itic.mall.system.inter.goods.skucoupon.persistence.model.SkuCouponExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class SkuCouponService
     extends CrudService<SkuCouponDao,SkuCoupon,SkuCouponExample> {
    @Autowired
    private SkuCouponDao skuCouponDao;
    
    public String updateSkuCoupon(List<Double> couponMobeys,List<String> spuIds){
    	String result = ResponseCode.SERVER_ERROR.getCode();
    	if(couponMobeys.size()>0 && spuIds.size()>0 && couponMobeys.size() == spuIds.size()){
    		for(int i=0;i<spuIds.size();i++){
    			List<SkuCoupon> list = getSkuCoupon(spuIds.get(i));
    			for(SkuCoupon skuCoupon:list){
    				//当需要修改的数值存在时才修改
    				if(couponMobeys.get(i) != null){
    					skuCoupon.setMoney(couponMobeys.get(i));
        				int back = this.skuCouponDao.updateByPrimaryKey(skuCoupon);
        				if(back == 1){
        					result = ResponseCode.SUCCESS.getCode();
        				}
    				}else{
    					result = ResponseCode.SUCCESS.getCode();
    				}
    			}
        	}
    	}
    	return result;
    }
    
    public String updateOneSkuCoupon(String spuId,Double couponMoney){
    	String result = ResponseCode.SERVER_ERROR.getCode();
    	if(StringHelper.isNotBlank(spuId) && couponMoney != null ){
    		SkuCoupon skuCoupon = geSkuCoupon(spuId);
    		skuCoupon.setMoney(couponMoney);
    		int back = this.skuCouponDao.updateByPrimaryKey(skuCoupon);
    		if(back == 1){
    			result = ResponseCode.SUCCESS.getCode();
    		}
    	}
    	return result;
    }
    
    public SkuCoupon geSkuCoupon(String spuId){
    	SkuCoupon skuCoupon = null;
    	if(StringHelper.isNotBlank(spuId)){
    		SkuCouponExample example = new SkuCouponExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andSpuIdEqualTo(spuId);
			List<SkuCoupon> list = this.skuCouponDao.selectByExample(example);
			if(list.size()>0){
				skuCoupon = list.get(0);
			}
    	}
    	return skuCoupon;
    }
    /**
     * 根据商品编号获取列表
     * @param spuId
     * @return
     */
    public List<SkuCoupon> getSkuCoupon(String spuId){
    	List<SkuCoupon> list = null;
    	if(StringHelper.isNotBlank(spuId)){
    		SkuCouponExample example = new SkuCouponExample();
			Criteria criteria = example.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andSpuIdEqualTo(spuId);
			list = this.skuCouponDao.selectByExample(example);
    	}
    	return list;
    }
    /**
     * 根据CouponId获取商品优惠信息
     * @author 王文鑫
     * @date 2018年1月11日 下午10:32:28
     * @param request
     * @param response
     * @param skuId
     * @return
     */
    public SkuCoupon getSkuCouponBySkuId(String CouponId) {
		return get(CouponId);
	}
    
    /**
     * 商品详情页领取券   券数量减少
     * @author 王文鑫
     * @date 2018年1月11日 下午10:35:31
     * @param request
     * @param response
     * @param id
     * @return
     */
	public boolean getCouponNumById(HttpServletRequest request, HttpServletResponse response, String id) {
		String result = ResponseCode.SERVER_ERROR.getCode();
		SkuCoupon skuCoupon = new SkuCoupon();
		boolean flag = false;
		
		//判断参数为空
		if(StringHelper.isNotBlank(id)){
			skuCoupon = get(id);//更具卷id获取卷的信息
			if(null != skuCoupon ){
				int skunum = skuCoupon.getTotalCount();//优惠券总数量（-1为无限制）
				if(skunum == -1){
					result = ResponseCode.SUCCESS.getCode();//返回成功
					flag = true ;
				}else if(skunum > 0 ){
					int skunum1 = NumericHelper.subInteger(skunum, 1);//优惠券数量-1
					skuCoupon.setTotalCount(skunum1);//设置优惠券数量参数
					result = edit(skuCoupon);//修改卷的信息
					flag = true ;
				}else{
					result = "优惠券信息为空";//优惠券信息为空
					flag = false ;
				}
			}else{
				result = "获取对象错误";//获取对象错误
				flag = false ;
			}
		}else{
			result = "缺少参数";//缺少参数
			flag = false ;
		}
		
		return flag;
	}

	public SkuCoupon getSkuCouponByGoodId(String goodsId) {
		SkuCouponExample example = new SkuCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andSpuIdEqualTo(goodsId);
		List<SkuCoupon> skuCoupons = this.list(example);
		if(skuCoupons.isEmpty())
			return null;
		else
			return skuCoupons.get(0);
	}
	
	public SkuCoupon getSkuCouponExpireTime(String couponId) {
		SkuCouponExample example = new SkuCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		criteria.andIdEqualTo(couponId);
		criteria.andStartTimeLessThan(new Date());
		criteria.andEndTimeGreaterThan(new Date());
		List<SkuCoupon> skuCoupons = this.list(example);
		if(skuCoupons.isEmpty())
			return null;
		else
			return skuCoupons.get(0);
	}
}